
# using System.Runtime.InteropServices;
# ```
 
# 2. 声明需要使用的Windows API函数：
 
# ```csharp
# [DllImport("user32.dll")]
# public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
 
# [DllImport("user32.dll")]
# public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
 
# [DllImport("user32.dll")]
# public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
 
# [DllImport("user32.dll")]
# public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
 
# [DllImport("user32.dll")]
# public static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
 
# [DllImport("user32.dll")]
# public static extern bool MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool bRepaint);
 
# [StructLayout(LayoutKind.Sequential)]
# public struct RECT
# {
#     public int Left;
#     public int Top;
#     public int Right;
#     public int Bottom;
# }
# ```
 
# 3. 在窗体的加载事件中，将Excel应用程序对象嵌入到窗体并设置相关属性：
 
# ```csharp
import excel12
from PyQt5 import QtWidgets
import win32gui
class MainW(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        QtWidgets.QMainWindow.__init__(self, parent)    
        w=QtWidgets.QWidget()
        h=QtWidgets.QHBoxLayout(w)
        self.b=QtWidgets.QPushButton("Embedded"            )
        self.b.clicked.connect(self.e_calc)
        h.addWidget(self.b)
        self.setCentralWidget(w)
    def e_calc(self):
        calc_hwnd = win32gui.FindWindow(None, u"计算器")
        hwin=int(self.winId())
        print(calc_hwnd,hwin)
        win32gui.SetParent(calc_hwnd, hwin)
    def Form1_Load(self,sender, EventArgs):
        excelApp = excel12.Application();
        workbook = excelApp.Workbooks.Add();
        # 获取Excel应用程序的主窗口句柄
        excelHwnd =excelApp.Hwnd
        # 获取窗体句柄
        # print(dir(self))
        formHwnd = self.winId();
        # 将Excel应用程序的父窗口设置为窗体
        win32gui.SetParent(excelHwnd, formHwnd);
        # 获取窗口样式
        style = win32gui.GetWindowLong(excelHwnd, -16);
        # 移除标题栏样式和边框样式
        style &= ~0xC00000; # WS_CAPTION | WS_THICKFRAME
        # 设置窗口样式
        win32gui.SetWindowLong(excelHwnd, -16, style);
        # 获取窗口客户区大小
        win32gui.GetClientRect(formHwnd, rect);
        # 调整Excel窗口大小以适应窗体
        win32gui.MoveWindow(excelHwnd, 0, 0, rect.Right - rect.Left, rect.Bottom - rect.Top, true);
if __name__ == "__main__":
    import sys

    a = QtWidgets.QApplication(sys.argv)
    a.lastWindowClosed.connect(a.quit)
    w = MainW()
    w.show()
    a.exec_()